package com.DataProcess;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.pudu.pdrobot.App;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by tjpu on 2016/10/18.
 */
public class PCDataProcess {
    static String thisLogTag = "PCDataProcess";

    public static void setGlobalValue(String strKey, String strValue)
    {
        try {
            Log.i(thisLogTag,strKey + " " + strValue);
            switch (strKey) {
                case "P_speed":
                    Robot.P_speed = Double.valueOf(strValue);
                    break;
                case "I_speed":
                    Robot.I_speed = Double.valueOf(strValue);
                    break;
                case "D_speed":
                    Robot.D_speed = Double.valueOf(strValue);
                    break;
                case "P_track":
                    Robot.P_track = Double.valueOf(strValue);
                    break;
                case "D_track":
                    Robot.D_track = Double.valueOf(strValue);
                    break;
                case "foresee":
                    Robot.foresee = Double.valueOf(strValue);
                    break;
                case "speed":
                    Robot.setSpeed = Double.valueOf(strValue);
                    break;
                case "K0":
                    Robot.K0 = Double.valueOf(strValue);
                    break;
                case "K1":
                    Robot.K1 = Double.valueOf(strValue);
                    break;
                case "K2":
                    Robot.K2 = Double.valueOf(strValue);
                    break;
                case "K3":
                    Robot.K3 = Double.valueOf(strValue);
                    break;
                case "K4":
                    Robot.K4  = Double.valueOf(strValue);
                    break;
                case "K5":
                    Robot.K5  = Double.valueOf(strValue);
                    break;
                case "a_b":
                    Robot.a_b  = Double.valueOf(strValue);
                    break;
                default:
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if (App.getInstance().canbusService == null || App.getInstance().canbusService.mCanDataHandle == null) {
            return;
        }else {
            Handler mHandler = App.getInstance().canbusService.mCanDataHandle.mHandler;
            Message msg = mHandler.obtainMessage();
            msg.what = 0x04;
            mHandler.sendMessage(msg);
        }
    }

    public static void getSpeedFromPC(String strKey, String strValue) {
        switch (strKey) {
            case "left":
                Robot.targetSpeed[0] = Double.valueOf(strValue);
                break;
            case "right":
                Robot.targetSpeed[1] = Double.valueOf(strValue);
                break;
            default:
        }
    }

    public static void getControlFromPC(String strKey, String strValue) {
        switch (strKey) {
            case "way":
                if(strValue.equals("teleop"))
                    Robot.isAuto = false;
                else if (strValue.equals("auto"))
                    Robot.isAuto = true;
                else ;
                break;
            case "power":
                if(strValue.equals("start"))
                    Robot.isEnable = true;
                else if (strValue.equals("stop"))
                    Robot.isEnable = false;
                else ;

                Log.i("isEnable","PC make isEnable"+Robot.isEnable);
                break;
            default:
        }
    }

    public static String getGlobalValue()
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type","get_value");
            jsonObject.put("P_speed", Robot.P_speed);
            jsonObject.put("I_speed", Robot.I_speed);
            jsonObject.put("D_speed", Robot.D_speed);
            jsonObject.put("P_track", Robot.P_track);
            jsonObject.put("D_track", Robot.D_track);
            jsonObject.put("foresee", Robot.foresee);
            jsonObject.put("speed", Robot.setSpeed);
            jsonObject.put("K0",Robot.K0);
            jsonObject.put("K1",Robot.K1);
            jsonObject.put("K2",Robot.K2);
            jsonObject.put("K3",Robot.K3);
            jsonObject.put("K4",Robot.K4);
            jsonObject.put("K5",Robot.K5);
            jsonObject.put("a_b",Robot.a_b);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }
}
